package org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.ddl;

import lombok.extern.slf4j.Slf4j;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.database.lambda.domain.PersistenceRepository;
import org.wu.framework.lazy.orm.database.lambda.stream.LambdaTableType;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.LazyOperationParameter;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author : Jia wei Wu
 * @version 1.0
 * describe :  执行 字符串 sql 脚本
 * @date : 2020/7/3 下午10:28
 */
@Slf4j
public class LazyOperationMethodStringScriptRunner extends AbstractLazyDDLOperationMethod {


    public LazyOperationMethodStringScriptRunner(LazyOperationParameter lazyOperationParameter) {
        super(lazyOperationParameter);
    }

    /**
     * @param sourceParams
     * @return description 通过参数获取持久性存储库对象
     * @author Jia wei Wu
     * @date 2021/4/17 3:38 下午
     **/
    @Override
    public PersistenceRepository doAnalyzePersistenceRepository(Object[] sourceParams) throws Exception {
        PersistenceRepository persistenceRepository = createPersistenceRepository();
        persistenceRepository.setExecutionType(LambdaTableType.STRING_SCRIPT_RUNNER);
        StringBuffer stringBuffer = new StringBuffer();
        for (Object sourceParam : sourceParams) {
            if (sourceParam instanceof String[]) {
                String[] stringScripts = (String[]) sourceParam;
                for (String script : stringScripts) {
                    stringBuffer.append(script);
                }
            } else {
                stringBuffer.append(sourceParam.toString());
            }
        }

        persistenceRepository.setQueryString("执行脚本sql:" + stringBuffer);
        return persistenceRepository;
    }

    /**
     * description 执行SQL 语句
     *
     * @param connection   连接
     * @param sourceParams 参数
     * @return 返回结果
     * @author Jia wei Wu
     * @date 2020/11/22 上午11:02
     */
    @Override
    public Object doExecute(Connection connection, Object[] sourceParams) throws Exception {
        if (ObjectUtils.isEmpty(sourceParams)) {
            return false;
        }
        // sql 脚本
        log.info("数据初始化开始: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Object sourceParam = sourceParams[0];
        // 创建 script 文件

        if (ObjectUtils.isEmpty(sourceParam)) {
            return false;
        }
        try {
            Statement statement = connection.createStatement();
            if (sourceParam instanceof String[] stringScripts) {
                for (String script : stringScripts) {
                    // 执行脚本
                    statement.execute(script);
                }
            } else {
                statement.execute(sourceParam.toString());
            }

        } catch (Exception e) {
            throw new SQLException("执行sql失败:" + sourceParam);
        }
        log.info("数据初始化结束: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return true;
    }

}
